// todo============= 顶部Tab栏============================
async function top_render() {
  const res = await axios.get('/index/index')
  // console.log(res);
  const newData = res.data.data.allCate
  // console.log(newData);
  document.querySelector('.container ul').innerHTML = newData.map((item) => {
    return `  
        <li class="menu-item menu-item-type-taxonomy menu-item-object-category " >
            <a title="" href="show.html" data-id="${item.id}"><span class="fa ${item.icon}"></span>${item.catename}</a>
        </li>
        `
  }).join('')
}
top_render()

$('#showReg').on('click', function () {
  location.href = './reg.html'
})

let form = layui.form;

form.verify({
  //我们既支持上述函数式的方式，也支持下述数组的形式
  //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]


});

$('.layui-form').on('submit', function (e) {
  // 阻止默认行为
  e.preventDefault()

  const data = $(this).serialize()
  console.log(data);

  // 4
  axios.post('/index/login', data)
    .then(({ data: res }) => {
      console.log(res);
      const { errno, data, errmsg } = res

      if (errno !== 0) {
        return layer.msg(errmsg)
      }

      // layer.msg(message)

      localStorage.setItem('token', data.token)
      // 存储注册数据
      async function getInfo() {
        const res = await axios.get('http://124.223.14.236:8060/user/getUserInfo')
        const { data: { data: { userInfo } } } = res
        localStorage.setItem('userInfo', JSON.stringify(userInfo))
      }
      getInfo()
      // console.log(token);
      // location.href='./user-add.html' 

      layer.msg(errmsg, {
        icon: 1,
        time: 1500 //2秒关闭（如果不配置，默认是3秒）
      }, function () {
        //do something
        location.href = './user-add.html'
      })
    })
})


